package cn.entity;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

/**
 * @author m on 2023/1/28
 * Description:文章
 */
@Entity
@Getter
@Setter
public class Article extends BaseEntity {

    private String title;
    private String content;
    private Integer commentSize = 0;
    @Temporal(TemporalType.TIMESTAMP)
    private Date createTime;
    @Temporal(TemporalType.TIMESTAMP)
    private Date updateTime;

    private boolean open;

    @ManyToOne
    private User user;

    @ManyToMany(cascade = {CascadeType.MERGE},fetch = FetchType.EAGER)
    @JoinTable(name = "article_tags")
    List<Tag> tags;

    //级联删除
    //@Where注解会在where后面自动添加语句 delete * from Comment where id = 1 (and pid=0)
    @OneToMany(mappedBy = "article",cascade = CascadeType.ALL,fetch = FetchType.LAZY)
    private List<Comment> comments;

    @JsonManagedReference
    public List<Comment> getComments() {
        return comments;
    }
}
